package com.dai.zhenfujin.visitingcard.app.adapter;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.dai.zhenfujin.Constants.Constants;
import com.dai.zhenfujin.R;
import com.dai.zhenfujin.bean.ProListBean;
import com.dai.zhenfujin.bean.UserBean;
import com.dai.zhenfujin.http.CallBack;
import com.dai.zhenfujin.http.HttpClient;
import com.dai.zhenfujin.issue.activity.AlumIssueActivity;
import com.dai.zhenfujin.issue.activity.AlumIssueActivity2;
import com.dai.zhenfujin.view.RoundImageView;
import com.daimajia.swipe.SwipeLayout;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PersonalDetailsListviewAdapter extends BaseAdapter {
    private Context context;
    private List<ProListBean.ProListBean2> beanList;
    private LayoutInflater inflater;

    public PersonalDetailsListviewAdapter(Context context, List<ProListBean.ProListBean2> beanList) {
        this.context = context;
        this.beanList = beanList;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return beanList.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if(convertView == null){
            holder = new PersonalDetailsListviewAdapter.ViewHolder();
            convertView = inflater.inflate(R.layout.personal_details_content,null);

            holder. ivImageviw = (RoundImageView)convertView.findViewById(R.id.pec_imageviw);//图片
            holder.  tvCompany = (TextView)convertView. findViewById(R.id.pec_company);//公司
            holder. tvPrice = (TextView) convertView.findViewById(R.id.pec_price);//价格
            holder.tvResidence = (TextView)convertView. findViewById(R.id.pec_residence);//类型
            holder. tvSite = (TextView) convertView.findViewById(R.id.pec_site);//地址
            holder. tv_delete = (TextView) convertView.findViewById(R.id.tv_delete);//地址
            holder. tv_edit = (TextView) convertView.findViewById(R.id.tv_edit);//地址
            holder. item_word_type = (TextView) convertView.findViewById(R.id.item_word_type);//
            holder. tv_time = (TextView) convertView.findViewById(R.id.tv_time);//
            holder. swipe_layout =  convertView.findViewById(R.id.swipe_layout);
            holder. bt_clear =  convertView.findViewById(R.id.bt_clear);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }


        final ProListBean.ProListBean2 bean = beanList.get(position);
        if (!TextUtils.isEmpty(bean.getPro_logo())){
            holder.swipe_layout.setVisibility(View.VISIBLE);
            holder.tvCompany.setText(bean.getPro_name());
            if (TextUtils.isEmpty(bean.getPro_price())){
                holder.tvPrice.setVisibility(View.INVISIBLE);
            }else {
               // holder.tvPrice.setVisibility(View.VISIBLE);
               // holder.tvPrice.setText( "￥" + bean.getPro_price() +"元");
            }
            holder.item_word_type.setText(bean.type_name);
            holder.tv_time.setText(bean.create_time.substring(0,bean.create_time.length()-2));
            //  holder.tvResidence.setText(bean.getResidence());
            holder.tvSite.setText(bean.getPro_address());
            Glide.with(context).load(bean.getPro_logo()).into(holder.ivImageviw);
        }else {
            holder.swipe_layout.setVisibility(View.GONE);
        }

         if(bean.pro_state == 1){
             holder. bt_clear.setText("审核通过");
         }else if(bean.pro_state == 2){
             holder. bt_clear.setText("未通过");
         }else if(bean.pro_state == 3){
             holder. bt_clear.setText("审核中");
         }else if(bean.pro_state == 4){
             holder. bt_clear.setText("过期");
         }else {
             holder. bt_clear.setText("业主已切换");
         }



        holder.tv_delete.setOnClickListener(v -> {
            Map<String, Object> params = new HashMap<>();
            Map<String, String> params2 = new HashMap<>();
            params2.put("pro_id",bean.getPro_id());  //名片
            params.put("req_bd",  params2);
            HttpClient.post(context, Constants.DELPRODUCT, params, new CallBack<UserBean>() {
                @Override
                public void onSuccess(UserBean resultBean) {
                    beanList.remove(bean);
                    notifyDataSetChanged();
                }
            });
        });

        holder.tv_edit.setOnClickListener(v -> {
            Intent intent = new Intent();
            intent.putExtra("bean", bean);
           // intent.putExtra("id",bean.getPro_id());
            intent.setClass(context, AlumIssueActivity2.class);
            context.startActivity(intent);
        });

        return convertView;
    }


    class ViewHolder{
        private RoundImageView ivImageviw;
        private TextView tvCompany;
        private TextView tvPrice,item_word_type,tv_time;
        private TextView tvResidence;
        private Button bt_clear;
        private SwipeLayout swipe_layout;
        private TextView tvSite,tv_delete,tv_edit;
    }
}
